package com.ruicar.afs.cloud.common.voucher.handle;

import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.common.datasync.exception.ParamSyncException;
import com.ruicar.afs.cloud.common.voucher.Voucher;
import com.ruicar.afs.cloud.common.voucher.VoucherGroup;
import com.ruicar.afs.cloud.common.voucher.VoucherRunResult;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
@AllArgsConstructor
public final class VoucherHelper  {
    private static final ParallelVoucherRunner parallelVoucherRunner = new  ParallelVoucherRunner();
    /**
     * Lock
     * <p>Description: </p>
     */
    private static  final Lock LOCK = new ReentrantLock();
    /**
     * Updating
     * <p>Description: </p>
     */
    private static boolean UPDATING = false;
    /**
     * VoucherRunResult
     * <p>Description: </p>
     *
     * @param paramMap 运行参数
     * @param list
     * @return the list
     */
    public static VoucherRunResult runVoucher(Map<String,Object> paramMap, List<Voucher> list){
         return parallelVoucherRunner.execute(list,paramMap);
    }
    /**
     * Update vouchers *
     *
     * @param vouchers json object
     */
    static void updateVouchers(JSONObject vouchers) throws ParamSyncException{
        UPDATING = true;
        LOCK.lock();
        try {
            if (vouchers == null) {
                log.warn("凭证同步失败，传入凭证为空");
                throw new ParamSyncException("凭证同步失败，传入凭证为空");
            }
            log.debug("==========开始同步凭证=========");
            VoucherCaches.resetCache();
            vouchers.keySet().stream().forEach(key -> {
                VoucherGroup voucherGroup = vouchers.getJSONObject(key).toJavaObject(VoucherGroup.class);
                VoucherCaches.addGroup(key, voucherGroup);
                if(voucherGroup.getVouchers()!=null){
                voucherGroup.getVouchers().forEach(voucher -> {
                    VoucherCaches.addVoucher(voucher.getRuleNo(), voucher);
                });}
                if(voucherGroup.getEvents()!=null&&voucherGroup.getEvents().size()>0){
                    VoucherCaches.addBuriedPointNo(voucherGroup.getEvents());
               }
                if(voucherGroup.getInfos()!=null) {
                    voucherGroup.getInfos().forEach(voucherBaseInfo -> {
                        VoucherCaches.addVoucherBaseInfo(voucherBaseInfo.getId(), voucherBaseInfo);
                    });
                }
            });
            log.debug("==========同步凭证结束=========");
        }finally {
            UPDATING = false;
            LOCK.unlock();
        }
    }
    /**
     * Check update
     */
    public static void checkUpdate(){
        if(UPDATING){
            LOCK.lock();
            try {
                while (UPDATING){
                    log.debug("凭证同步中等待同步结束");
                    ThreadUtil.sleep(100);
                }
            }finally {
                LOCK.unlock();
            }
        }
    }



}
